﻿using System;
using System.Linq;
using MvvmCodeReduction.Model;
using MvvmCodeReduction.View.MVP;

namespace MvvmCodeReduction.Presenter
{
    public class CustomersPresenter
    {
        public const string CancelButtonText = "Cancel";
        public const string EditButtonText = "Edit";
        public const string EditButtonName = "EditButton";
        public const string EditTextBoxName = "EditTextBox";
        public const string SaveButtonName = "SaveButton";
        private ICustomersViewMVP _view;
        private IRepository _repository;
        public const string NameTextBlockName = "NameTextBlock";

        #region Constructor

        public CustomersPresenter(ICustomersViewMVP view,
                                  IRepository repository)
        {
            Initialize(view, repository);
        }

        protected void Initialize(ICustomersViewMVP view,
                                  IRepository repository)
        {
            if (view == null)
                throw new ArgumentNullException("view");
            if (repository == null)
                throw new ArgumentNullException("repository");

            _view = view;
            _view.OnEdit += _view_OnEdit;
            _view.OnCancel += _view_OnCancel;
            _view.OnSave += _view_OnSave;
            _view.CancelText = CancelButtonText;
            _repository = repository;
            _view.Customers = _repository.GetCustomers();
        }

        #endregion Constructor


        #region Event Handlers

        private void _view_OnSave(object sender,
                                  CustomerEventArgs e)
        {
            if (!_view.DisableControl(SaveButtonName, e.Customer.ID))
                _view.Alert("Unable to disable " + SaveButtonName);
            if (!_view.SetControlContent(EditButtonName, e.Customer.ID, EditButtonText))
                _view.Alert("Unable to set content for " +
                            EditButtonName);
            if (!_view.HideControl(EditTextBoxName, e.Customer.ID))
                _view.Alert("Unable to hide control " +
                            EditTextBoxName);
            if (!_view.ShowControl(NameTextBlockName, e.Customer.ID))
                _view.Alert("Unable to show control " +
                            NameTextBlockName);
            Model.Customer customer
                = _view.Customers.FirstOrDefault(
                    c => c.ID == e.Customer.ID);
            if (customer == null)
            {
                _view.Alert("Unable to update customer");
                return;
            }
            string editedName;
            if (!_view.GetTextBoxText(EditTextBoxName,
                                        e.Customer.ID, out editedName))
            {
                _view.Alert("Unable to get content of " + EditTextBoxName);
                return;
            }
            customer.Name = editedName;
        }

        private void _view_OnEdit(object sender,
                                  CustomerEventArgs e)
        {
            if (!_view.SetControlContent(EditButtonName, e.Customer.ID,
                                    CancelButtonText))
                _view.Alert("Unable to change " + EditButtonName + " text.");
            if (!_view.EnableControl(SaveButtonName, e.Customer.ID))
                _view.Alert("Unable to enable " + SaveButtonName);
            if (!_view.ShowControl(EditTextBoxName, e.Customer.ID))
                _view.Alert("Unable to show " + EditTextBoxName);
            if (!_view.HideControl(NameTextBlockName, e.Customer.ID))
                _view.Alert("Unable to hide " + NameTextBlockName);
            if (!_view.SetTextBoxText(EditTextBoxName, e.Customer.ID,
                                 e.Customer.Name))
                _view.Alert("Unable to set text for " + EditTextBoxName);
        }

        private void _view_OnCancel(object sender,
                                  CustomerEventArgs e)
        {
            if (!_view.SetControlContent(EditButtonName, e.Customer.ID,
                                    EditButtonText))
                _view.Alert("Unable to set text for " +
                            EditButtonName);
            if (!_view.HideControl(EditTextBoxName, e.Customer.ID))
                _view.Alert("Unable to hide " + EditTextBoxName);
            if (!_view.ShowControl(NameTextBlockName, e.Customer.ID))
                _view.Alert("Unable to show " + NameTextBlockName);
            if (!_view.SetTextBoxText(EditTextBoxName, e.Customer.ID,
                                 string.Empty))
                _view.Alert("Unable to set text for " +
                            EditTextBoxName);
        }

        #endregion Event Handlers
    }
}
